<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="折扣方案名字" prop="discountName">
      <el-input v-model="dataForm.discountName" placeholder="折扣方案名字"></el-input>
    </el-form-item>
    <el-form-item label="参加时段" prop="timeIntervalList">
		<el-checkbox-group v-model="dataForm.timeIntervalList">
		  <el-checkbox v-for="time in timeIntervalList" :key="time.timeIntervalId" :label="time.timeIntervalId">{{ time.timeIntervalName }}</el-checkbox>
		</el-checkbox-group>
    </el-form-item>
    <el-form-item label="权限角色" prop="roleList">
		<el-checkbox-group v-model="dataForm.roleList">
		  <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{ role.roleName }}</el-checkbox>
		</el-checkbox-group>
    </el-form-item>
    <el-form-item label="状态" prop="status">
	  <el-radio v-model="dataForm.status" label="1">启用</el-radio>
	  <el-radio v-model="dataForm.status" label="2">停用</el-radio>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
				timeIntervalList:[],
				roleList:[],
        visible: false,
        dataForm: {
          discountId: 0,
          discountName: '',
          sellerId: '',
          timeIntervalId: '',
          authRoleList: '',
          status: '1',
          discountMode: '',
          remark: '',
		  timeIntervalList:[],
		  roleList:[],
        },
        dataRule: {
          discountName: [
            { required: true, message: '折扣方案名字不能为空', trigger: 'blur' }
          ],
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          timeIntervalList: [
            { required: true, message: '参加时段不能为空', trigger: 'blur' }
          ],
          roleList: [
            { required: true, message: '权限角色不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1启用2停用)不能为空', trigger: 'blur' }
          ],
          discountMode: [
            { required: true, message: '折扣方案不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
				//参加时段
				this.$http({
				  url: this.$http.adornUrl('/store/tstoretimeinterval/list'),
				  method: 'get',
				}).then(({data}) => {
				  if (data && data.code === 0) {
				    this.timeIntervalList = data.page.list
				  } else {
				    this.timeIntervalList = []
				  }
				})
		//权限角色
		this.$http({
		  url: this.$http.adornUrl('/sys/role/select'),
		  method: 'get',
		  params: this.$http.adornParams()
		}).then(({data}) => {
		  this.roleList = data && data.code === 0 ? data.list : []
		})

        this.dataForm.discountId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.discountId) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorediscount/info/${this.dataForm.discountId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.discountName = data.tStoreDiscount.discountName
                this.dataForm.sellerId = data.tStoreDiscount.sellerId
                this.dataForm.timeIntervalList = data.tStoreDiscount.timeIntervalList
                this.dataForm.roleList = data.tStoreDiscount.roleList
                this.dataForm.status = data.tStoreDiscount.status.toString()
                this.dataForm.discountMode = data.tStoreDiscount.discountMode
                this.dataForm.remark = data.tStoreDiscount.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/store/tstorediscount/${!this.dataForm.discountId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'discountId': this.dataForm.discountId || undefined,
                'discountName': this.dataForm.discountName,
                'sellerId': this.dataForm.sellerId,
                'timeIntervalList': this.dataForm.timeIntervalList,
                'roleList': this.dataForm.roleList,
                'status': this.dataForm.status.toString(),
                'discountMode': this.dataForm.discountMode,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
